package com.adtech.farmProducts.base;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.adtech.common.http.AsyncHttpResponse;
import com.adtech.common.model.RspModel;
import com.adtech.common.utils.AccountManagers;
import com.adtech.common.widgets.dialog.LoadingDialog;
import com.adtech.common.widgets.load.core.LoadManager;
import com.adtech.farmProducts.App;
import com.adtech.farmProducts.R;
import com.adtech.farmProducts.widget.ErrorView;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.gyf.barlibrary.ImmersionBar;
import com.vise.log.ViseLog;

import java.lang.reflect.Type;

/**
 * Descr:   Fragment碎片的基类
 * Date:    2018/1/19.
 */

public abstract class BaseFragment extends Fragment {

    private LoadingDialog loadingDialog;

    /**
     * @param canBack true 可以取消 false 不可以取消 设置在点击Dialog外是否取消Dialog进度条
     */
    public void loadingDialogShow(boolean canBack) {
        if (loadingDialog != null) {
            loadingDialog.cancel();
        } else {
            loadingDialog = new LoadingDialog(getActivity());
        }
        loadingDialog.show(canBack, R.layout.layout_state_loading);
    }

    public void onDismiss() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }


    @Override
    protected void initWidgets(View rootView) {
        super.initWidgets(rootView);
        View top_view = rootView.findViewById(R.id.top_view);
        if (top_view != null) {
            ImmersionBar.setTitleBar(getActivity(), top_view);
        }
    }

    /**
     * 数据解析
     *
     * @param response
     * @param type
     * @param log
     * @param <T>
     * @return
     */
    public<T> RspModel<T> getAnalysis(AsyncHttpResponse response, Type type, String log) {
        ViseLog.d(response.toString());
        switch (response.getCode()) {
            case 200:
                try {
                    if (response.getBody().indexOf("\"code\":401") == -1) {//说明没有该参数
                        RspModel<T> rspModel = new Gson().fromJson(response.getBody(), type);
                        ViseLog.e(log + rspModel.toString());
                        switch (rspModel.getCode()) {
                            case 1:
                                return rspModel;
                            case 401://用户信息为通过验证
                                AccountManagers.logOut();
                                App.getInstance().showMessage("请重新登录");
                                break;
                            default:
                                App.getInstance().showMessage(rspModel.getCode() + rspModel.getMessage());
                                break;
                        }
                    } else {
                        AccountManagers.logOut();
                        App.getInstance().showMessage("请重新登录");
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
                break;
        }
        return null;
    }

    /**
     * 数据解析
     *
     * @param response
     */
    public <T> RspModel<T> getAnalysisBase(LoadManager mLoadManager, AsyncHttpResponse response, Type type, String log) {
        ViseLog.d(response.toString());
        switch (response.getCode()) {
            case 200:
                try {
                    RspModel<T> rspModel = new Gson().fromJson(response.getBody(), type);
                    ViseLog.e(log + rspModel.toString());
                    switch (rspModel.getCode()) {
                        case 1:
                            mLoadManager.showSuccessView();
                            return rspModel;
                        default:
                            mLoadManager.showStateView(ErrorView.class);
                            break;
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
                break;
            default:
                mLoadManager.showStateView(ErrorView.class);
                break;
        }
        return null;
    }

    /**
     * 隐藏软键盘
     **/
    public void hideKeyboard() {
        View view = getActivity().getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
